Skip to content

Method: static {...}

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
5: * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
6: * version.
7: * <p>
8: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
9: * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
10: * details. You should have received a copy of the GNU General Public License along with this program. If not, see
11: * <http://www.gnu.org/licenses/>.
12: */
13: package cz.cvut.kbss.jopa.model;
14:
15: import cz.cvut.kbss.jopa.exception.MetamodelInitializationException;
16: import cz.cvut.kbss.jopa.exceptions.OWLPersistenceException;
17: import cz.cvut.kbss.jopa.loaders.EntityLoader;
18: import cz.cvut.kbss.jopa.model.metamodel.*;
19: import cz.cvut.kbss.jopa.utils.Configuration;
20: import cz.cvut.kbss.ontodriver.config.OntoDriverProperties;
21: import org.slf4j.Logger;
22: import org.slf4j.LoggerFactory;
23:
24: import java.lang.reflect.Field;
25: import java.net.URI;
26: import java.net.URISyntaxException;
27: import java.util.*;
28:
29: public class MetamodelImpl implements Metamodel {
30:
31: private static final Logger LOG = LoggerFactory.getLogger(Metamodel.class);
32:
33: private static final String ASPECTJ_CLASS = "org.aspectj.weaver.loadtime.Agent";
34:
35: private final Map<Class<?>, EntityType<?>> typeMap = new HashMap<>();
36:
37: private final Set<Class<?>> inferredClasses = new HashSet<>();
38:
39: private final Configuration configuration;
40:
41: private Set<URI> moduleExtractionSignature;
42:
43: private final Set<Class<?>> entities = new HashSet<>();
44:
45: public MetamodelImpl(Configuration configuration, EntityLoader entityLoader) {
46: this.configuration = Objects.requireNonNull(configuration);
47: Objects.requireNonNull(entityLoader);
48: build(entityLoader);
49: }
50:
51: private void build(EntityLoader entityLoader) {
52: LOG.debug("Building metamodel ... ");
53: checkForWeaver();
54:
55: loadEntities(entityLoader);
56:
57: entities.forEach(c -> processOWLClass(c));
58: }
59:
60: /**
61: * Check the class path for aspectj weaver, which is vital for using lazy loading.
62: */
63: private void checkForWeaver() {
64: try {
65: MetamodelImpl.class.getClassLoader().loadClass(ASPECTJ_CLASS);
66: } catch (ClassNotFoundException e) {
67: LOG.error("AspectJ not found on classpath. Cannot run without AspectJ.");
68: throw new OWLPersistenceException(e);
69: }
70: }
71:
72: private void loadEntities(EntityLoader entityLoader) {
73: Set<Class<?>> discoveredEntities = entityLoader.discoverEntityClasses(configuration);
74: entities.addAll(discoveredEntities);
75: }
76:
77: private <X> void processOWLClass(final Class<X> cls) {
78: if (typeMap.containsKey(cls)) {
79: return;
80: }
81:
82: LOG.debug("processing OWL class: {}", cls);
83:
84: final EntityClassProcessor classProcessor = new EntityClassProcessor();
85:
86: final EntityTypeImpl<X> et = classProcessor.processEntityType(cls);
87:
88: typeMap.put(cls, et);
89: final EntityFieldMetamodelProcessor<X> fieldProcessor = new EntityFieldMetamodelProcessor<>(cls, et, this);
90:
91: for (final Field field : cls.getDeclaredFields()) {
92: fieldProcessor.processField(field);
93: }
94:
95: if (et.getIdentifier() == null) {
96: throw new MetamodelInitializationException("Missing identifier field in entity class " + cls);
97: }
98: }
99:
100: @SuppressWarnings("unchecked")
101: @Override
102: public <X> EntityType<X> entity(Class<X> cls) {
103: if (!typeMap.containsKey(cls)) {
104: processOWLClass(cls);
105: }
106:
107: return (EntityType<X>) typeMap.get(cls);
108: }
109:
110: @Override
111: public <X> EmbeddableType<X> embeddable(Class<X> cls) {
112: throw new IllegalArgumentException("Embeddable entities not supported.");
113: }
114:
115: @Override
116: public Set<EmbeddableType<?>> getEmbeddables() {
117: return Collections.emptySet();
118: }
119:
120: @Override
121: public Set<EntityType<?>> getEntities() {
122: return new HashSet<>(typeMap.values());
123: }
124:
125: @Override
126: public Set<ManagedType<?>> getManagedTypes() {
127: return new HashSet<>(typeMap.values());
128: }
129:
130: @Override
131: public <X> ManagedType<X> managedType(Class<X> cls) {
132: return entity(cls);
133: }
134:
135: @Override
136: public Set<Class<?>> getInferredClasses() {
137: return Collections.unmodifiableSet(inferredClasses);
138: }
139:
140: public void addInferredClass(Class<?> cls) {
141: inferredClasses.add(cls);
142: }
143:
144: @Override
145: public Set<URI> getModuleExtractionExtraSignature() {
146: return Collections.unmodifiableSet(getSignatureInternal());
147: }
148:
149: @Override
150: public void addUriToModuleExtractionSignature(URI uri) {
151: if (uri == null) {
152: throw new NullPointerException();
153: }
154: synchronized (this) {
155: getSignatureInternal().add(uri);
156: }
157: }
158:
159: private synchronized Set<URI> getSignatureInternal() {
160: // This can be lazily loaded since we don'attributeType know if we'll need it
161: if (moduleExtractionSignature == null) {
162: final String sig = configuration.get(OntoDriverProperties.MODULE_EXTRACTION_SIGNATURE);
163: if (sig == null) {
164: this.moduleExtractionSignature = new HashSet<>();
165: } else {
166: final String[] signature = sig.split(OntoDriverProperties.SIGNATURE_DELIMITER);
167: this.moduleExtractionSignature = new HashSet<>(signature.length);
168: try {
169: for (String uri : signature) {
170: moduleExtractionSignature.add(new URI(uri));
171: }
172: } catch (URISyntaxException e) {
173: throw new OWLPersistenceException("Invalid URI encountered in module extraction signature.", e);
174: }
175: }
176: }
177: return moduleExtractionSignature;
178: }
179: }